<%@ page contentType="text/html; charset=UTF-8"%>
<%@page import="edu.ccut.saturn.component.SaturnData"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%@ include file="/extjs/include/all.jsp"%>
<title></title>
<script type="text/javascript">
	Ext.onReady(function(){
	var resultWidth = Ext.get("task-grid").getWidth();
		var userId = '${__SATURN_AUTORITY_USER__.id}';
		var formPanel = new Ext.FormPanel({
			frame: true,
	        title:'委托完成的任务',
	        labelWidth: 60,
	        width:resultWidth,
	        renderTo:'form-ct',
	        bodyStyle:'padding:5px 5px 0;width:100%',
			items: [{
	              xtype:'fieldset',
           		  title: '选项',
          		  autoHeight: true,
          		    width:resultWidth,
            	  layout: 'form',
		     	  items: [{
	                xtype: 'checkboxgroup',
	                fieldLabel: '任务来源',
	                itemCls: 'x-check-group-alt',
	                columns: 3,
	                items: [
	                 {boxLabel: '代理任务', id: 'cbAgentTask', checked: true},
	                 {boxLabel: '代办任务', id: 'cbDelegateTask', checked: true},
	                 {boxLabel: '协办任务', id: 'cbHelpTask',checked: true}
	                ]
            	}]
            }],
             buttons: [{
	            text: '查询',
	            handler:function(){
	            	if(isValid()){            	
	            		var task = getBizState();
	            		store.load({params:{	
	            						start:0,
	            						limit:20,
	            						state:12,			
										userid:userId,
										bizstate:task }}); 
		
		       			grid.getView().refresh();
	            	}else{
	            		Ext.MessageBox.alert("请选择任务来源");
	            	}
	            }
       		 }]
		});
		
		function getBizState(){
			var cbAgentTask = Ext.get("cbAgentTask").dom.checked;
			var cbDelegateTask = Ext.get("cbDelegateTask").dom.checked;
			var cbHelpTask = Ext.get("cbHelpTask").dom.checked;
			var task = "";
			if(cbAgentTask){
       		   task = task + "1,";
       		}
       		if(cbDelegateTask){
       			task = task + "2,";
       		}
       		if(cbHelpTask){
       			task = task + "3,";
       		}
       		return task.substring(0,task.length-1);
		}
		
		function isValid(){
			var flag = true;
			var cbAgentTask = Ext.get("cbAgentTask").dom.checked;
			var cbHelpTask = Ext.get("cbHelpTask").dom.checked;
			var cbDelegateTask = Ext.get("cbDelegateTask").dom.checked;
			if(cbAgentTask==false&&cbHelpTask==false&&cbDelegateTask==false){
			   flag = false;
			}
			return flag;
		}
		
		var store = new Ext.data.JsonStore({
		    url: '${pageContext.request.contextPath}/edu.ccut.saturn.workflow.client::/myTaskModule/action/QueryDelegateFinTasks.action',
		    root: 'result',
		    totalProperty:'count',
		    fields: [
	               {name: 'workitem_name', mapping: 'workitem_name'},
	               {name: 'process_instance_id', mapping: 'process_instance_id'},
		    	   {name: 'workitem_id', mapping: 'workitem_id'},
	               {name: 'process_name', mapping: 'process_name'},
	               {name: 'process_instance_name', mapping: 'process_instance_name'},
	               {name: 'workitem_state', mapping: 'workitem_state'},
	               {name: 'workitem_is_timeout', mapping: 'workitem_is_timeout'},
	               {name: 'workitem_start_time', mapping: 'workitem_start_time'},
	               {name: 'limit_number', mapping: 'limit_number'}
	           ]
		});
	  
	    var grid = new Ext.grid.GridPanel({
       	    store: store,
	        columns: [
	       	    {header: "操作", width: 120, dataIndex: 'workitem_id', sortable: false,renderer:showOp},
	            {header: "工作项名称", width: 120, dataIndex: 'workitem_name', sortable: true},
	            {header: "业务流程名称", width: 120, dataIndex: 'process_name', sortable: true},
	            {header: "流程实例名称", width: 180, dataIndex: 'process_instance_name', sortable: true},
	            {header: "当前状态", width: 115, dataIndex: 'workitem_state', sortable: true,renderer:showState},
	            {header: "启动时间", width: 120, dataIndex: 'workitem_start_time', sortable: true},
	             {header: "是否超时", width: resultWidth*0.1, dataIndex: 'workitem_is_timeout', sortable: true,renderer:showIsTimeout},
	            {header: "时间限制", width: 120, dataIndex: 'limit_number', sortable: true}
	        ],
	        renderTo:'task-grid',
	        height:400,
	        width:resultWidth,
	        loadMask: {msg:'正在加载数据，请稍侯……'}, 
	        frame:true,
	        bodyStyle:'padding:5px 5px 0;width:100%',
	        bbar: new Ext.PagingToolbar({ 
		            pageSize: 20, 
		            store: store, 
		            displayInfo: true, 
		            displayMsg: '当前显示 {0} - {1}条记录 /共 {2}条记录',
		            emptyMsg: "没有数据显示" 
		    }),
		     tbar:new Array(
			   new Ext.Toolbar.Button({
						   text:'只显示超时任务',
						   xtype:"button" ,
						   tooltip:"只显示超时任务" ,
						   handler: queryTaskOfisTimeout,
						   scope :this})
			   
		   )  
    });
    
    var str = "2,3";
   	store.load({params: {start:0,limit:20,userid:userId,bizstate:str}});
   	
  	store.on('beforeload',function(){
  		if(isValid()){           
        	var task = getBizState();
	     	Ext.apply(this.baseParams,{userid:userId,bizstate:task});
        }else{
        	Ext.MessageBox.alert("请选择任务来源");
        	return false;
        }   
    });
    
    function queryTaskOfisTimeout(){
		store.load({params: {workitem_is_timeout:'Y'}});
	}
	
   	function showOp(value,cellmeta,record){	
			return "<a href='javascript:doView("+record.data["process_instance_id"]+")'>"+"查看"+"</a>";	
	}		
});

	function doView(process_instance_id){
		 var form = document.forms[0];
		 form.action="${pageContext.request.contextPath}/edu.ccut.saturn.workflow::/publicModule/action/DelineateWorkFlow.action?process_instance_id="+process_instance_id;
		 form.submit();
	}
	function showState(value){
		if(value=='10')
			return "运行";
		if(value=='12')
			return "完成";
	}
	
	function showIsTimeout(value){
		if(value=="Y"){
			return "<font color='red'>"+"是"+"</font>"
		}else{
			return "否";
		}
	}
</script>
<div id='form-ct' width='100%'></div>
<div id='task-grid' width='100%'></div>
</head>
</html>